package top.soulblack.purchasesale.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.mapper.*;
import top.soulblack.purchasesale.model.WechatTableClassify;
import top.soulblack.purchasesale.model.WechatTableColumn;
import top.soulblack.purchasesale.model.WechatTablePage;

import java.util.*;

@Service
@Slf4j
public class WechatPageComponentService {
    @Autowired
    private WechatTablePageMapper wechatTablePageMapper;
    @Autowired
    private WechatTableColumnMapper wechatTableColumnMapper;
    @Autowired
    private WechatAccessService wechatAccessService;
    @Autowired
    private WechatTableClassifyMapper wechatTableClassifyMapper;

    /***
     * @Author li
     * @Description 获取父id下的组件信息
     * @Date 10:05 2019/8/14
     * @Param [pid]
     * @return java.util.List<top.soulblack.purchasesale.model.WechatTablePage>
     */
    public List<WechatTablePage> getPageComponents(Integer pid,Map<String,String[]> constraints){
        List<WechatTablePage> components = wechatTablePageMapper.selectComponentByPid(pid);
        //关联数据表内容缓存
        Map<Integer,List<Map<String,Object>>> cache = new LinkedHashMap<Integer,List<Map<String,Object>>>();
        //关联数据表项缓存索引为其id
        Map<Integer,WechatTableColumn> linkColCache = new LinkedHashMap<Integer, WechatTableColumn>();
        //table_id为索引
        Map<Integer,WechatTableClassify> tableCache = new LinkedHashMap<Integer, WechatTableClassify>();
            for (WechatTablePage iterator:components){
                if(iterator.getPaddingData()!=0){
                    //获取数据表名
                    WechatTableClassify wechatTableClassify;
                    if(tableCache.containsKey(iterator.getImpactTable())){
                         wechatTableClassify = tableCache.get(iterator.getImpactTable());
                    }else {
                         wechatTableClassify = wechatTableClassifyMapper.selectByPrimaryKey(iterator.getImpactTable());
                         tableCache.put(wechatTableClassify.getId(),wechatTableClassify);
                    }
                    //获取所需提取的表项
                    List<String> columns= new ArrayList<String>();
                    for (WechatTableColumn column:iterator.getColumns()){
                        columns.add(column.getName());
                    }
                    //提取约束条件
                    List<String>condiction = new ArrayList<String>();
                    for(WechatTableColumn column:iterator.getColumns()){
                        if(constraints.containsKey(column.getName())){
                            switch (column.getType()){
                                case "INTEGER":
                                case "DECIMAL": condiction.add('`'+column.getName()+'`' +'='+constraints.get(column.getName())[0]);break;
                                case "VARCHAR": condiction.add('`'+column.getName()+'`' +" like "+"\'%"+constraints.get(column.getName())[0]+"%\'");break;
                            }
                        }
                    }
                    List<Map<String,Object>> data = wechatAccessService.selectList(wechatTableClassify,columns,condiction,new ArrayList<String>());
                    iterator.setData(data);
                }
                for (WechatTableColumn column:iterator.getColumns()){
                    if (column.getLinkId()!=null){
                        //如果缓存了关联节点
                        if(linkColCache.containsKey(column.getLinkId())){
                            column.setLinkColumn(linkColCache.get(column.getLinkId()));
                        }else{
                            WechatTableColumn linkColumn = wechatTableColumnMapper.selectByPrimaryKey(column.getLinkId());
                            linkColCache.put(column.getLinkId(),linkColumn);
                            column.setLinkColumn(linkColumn);
                        }
                        //如果缓存了数据
                        if(cache.containsKey(column.getLinkId())){
                            column.setData(cache.get(column.getLinkId()));
                        }else {
                            WechatTableClassify childTable;
                            if(tableCache.containsKey(column.getLinkColumn().getPid())){
                                childTable = tableCache.get(iterator.getImpactTable());
                            }else {
                                childTable = wechatTableClassifyMapper.selectByPrimaryKey(column.getLinkColumn().getPid());
                                tableCache.put(childTable.getId(),childTable);
                            }
                            List<Map<String,Object>> data = wechatAccessService.selectList(childTable,
                                    Arrays.asList(childTable.getIndex(),column.getLinkColumn().getName()),new ArrayList<String>(),new ArrayList<String>());
                            column.setData(data);
                            cache.put(column.getLinkId(),data);
                        }
                    }
                }
            }
        return components;
    }

    public List<WechatTablePage> getPageComponents(Integer pid){
        return this.getPageComponents(pid,new HashMap<String, String[]>());
    }
}
